import {
    getSocialAnalysisDetail,
    saveMonitoringInstance,
    addGroup,
    getSocialAnalysisCount,
    getAllTags,
    tagsSave,
    tagsSaveOrUpdate,
    getPostingStatisticsData,
    delTags,
    deleteOwnerTag,
    getOwnerTags,
    getsubscribeTree
} from '@/api/archives'
import util from '@/utils'
// 异步引入组件
const PostingChart = () => import('./components/posting-chart.vue')
const ArticleList = () => import('./components/article-list.vue')
const HotwordCloud = () => import('./components/hotword-cloud.vue')
const SensitivewordCloud = () => import('./components/sensitiveword-cloud.vue')
// 文档及多媒体添加组件
const TagManagement = () => import('./components/group-management.vue')
export default {
    data () {
        return {
            rowkey: '',
            objectUri: '',
            // 标签
            tags: [],
            // 用户头像
            obj_icon: '',
            // 名称
            displayName: '',
            // 类别
            name: '',
            // 所属分组
            belongGroup: '',
            // 社交数量统计信息列表
            socialAnalysisCountList: '',
            // 文章列表数据
            articleList: [],
            // 热词列表数据
            heatwordsList: [],
            // 关键词列表数据
            keywordsList: [],
            // 当前所拥有的标签
            ownerTags: [],
            // 所有标签
            allTags: [],
            // 过滤后的标签
            fiterTags: [],
            // 发帖柱状图X轴数据
            posting_xData: [],
            // 发帖柱状图数据
            posting_seriesData: [],
            postingChart_Visible: false,
            addTag_visible: false,
            // 当前输入的标签
            currentTag: '',

            // 左侧菜单树数据
            treeData: [],
            // 树节点是否展开
            expandAll_flag: true,
            // 设置标签对话框是否展示
            setTagsDialog_Visible: false,
            // 关联实例列表
            instanceList: [],
            // 关联实例列表对话框是否显示
            intanceListDialog_Visible: false,
            // 关联实例列表对话框标题
            intanceListDialog_title: '',
            moveToGroup_dialog: TagManagement,
            // 分组弹窗是否展示
            moveDialogVisible: false
        }
    },
    components: {
        PostingChart,
        ArticleList,
        HotwordCloud,
        SensitivewordCloud,
        TagManagement
    },
    created () {
        this.rowkey = this.$route.query.instanceUri
        this.objectUri = this.$route.query.objectUri
        this.name = this.$route.query.name
        this.displayName = this.$route.query.displayName
        this.obj_icon = this.$route.query.icon
        this.obj_type = this.$route.query.objectType
        this.belongGroup = this.$route.query.belongGroup
    },
    computed: {
        tagsCheckedList: function () {
            let a = []
            this.allTags.forEach(item => {
                if (item.isChecked) a.push(item.displayname)
            })
            return a
        }
    },
    mounted () {
        // 监听浏览器尺寸变化,重新渲染图表
        window.addEventListener('resize', this.handleResize, false)
        this.getSocialAnalysisCount()
        this.getOwnerTags()
        this.getPostingStatisticsData('year')
        // 获取左侧树数据
        this.loadTree()
        this.init_obj()
    },
    methods: {
        // 关闭设置标签弹窗
        handleCloseTagsDialog () {
            this.setTagsDialog_Visible = false
        },
        // 初始化标题
        init_obj () {
            let otype = ''
            let type = this.obj_type
            if (type.localeCompare('com.scistor.object.entity') == 0) {
                otype = '实体'
            }
            if (type.localeCompare('com.scistor.object.event') == 0) {
                otype = '事件'
            }
            document.title = this.displayName
            this.obj_title = `${this.displayName}(${this.name})-${otype}`
        },
        // 保存监控实例
        saveMonitoringInstance () {
            let params = [
                {
                    rowkey: this.rowkey,
                    objectUri: this.objectUri
                }
            ]
            // if (util.addToMonitoring(this.rowkey)) {
            saveMonitoringInstance(params)
                .then(response => {
                    this.$message.success('添加成功')
                })
                .catch(() => {
                    this.$message.error('已添加到监控，请勿重复添加!')
                })
            // }
        },
        // 加载左侧菜单树
        loadTree () {
            getsubscribeTree().then(response => {
                this.treeData = util.parseTreeData(response.data)
            })
        },
        getSocialAnalysisCount () {
            getSocialAnalysisCount({ rowkey: this.rowkey, objectUri: this.objectUri }).then(response => {
                this.socialAnalysisCountList = response.data
            })
        },
        // 获取关联实例列表
        getSocialAnalysisDetail (linkUri, title, direction) {
            getSocialAnalysisDetail({
                rowkey: this.rowkey,
                objectUri: this.objectUri,
                linkUri: linkUri,
                direction: direction
            }).then(response => {
                this.instanceList = response.data.list
            })
            this.intanceListDialog_title = title + '列表'
            this.intanceListDialog_Visible = true
        },
        // 获取发帖统计分析数据
        getPostingStatisticsData (type) {
            this.postingChart_Visible = false
            let params = {
                rowkey: this.rowkey,
                objectUri: this.objectUri,
                type: type
            }
            getPostingStatisticsData(params).then(response => {
                this.posting_seriesData = []
                // 如果为重要人物则显示折线图，否则为柱状图
                if (this.belongGroup === 'keyfigures' || this.belongGroup === 'focusgroup') {
                    let accountPostList = response.data.accountPostList
                    accountPostList.forEach(item => {
                        let postingData = item.postCount
                        // 根据对象key排序对象
                        postingData = util.objKeySort(postingData)
                        this.posting_xData = Object.keys(postingData)
                        this.posting_seriesData.push({
                            name: item.type,
                            type: 'line',
                            data: Object.values(postingData)
                        })
                    })
                } else {
                    let postingData = response.data.postCount
                    // 根据对象key排序对象
                    postingData = util.objKeySort(postingData)
                    this.posting_xData = Object.keys(postingData)
                    this.posting_seriesData = [
                        {
                            name: '发帖数',
                            type: 'bar',
                            barWidth: '50%',
                            data: Object.values(postingData)
                        }
                    ]
                }
                this.postingChart_Visible = true
            })
        },
        // 修改发帖统计分析时间类型
        modifyDateType (type) {
            this.getPostingStatisticsData(type)
        },
        // 获取所有标签
        getAllTags () {
            getAllTags().then(response => {
                this.allTags = response.data.map(item => {
                    item.isChecked = false
                    return item
                })
                // 过滤当前对象所拥有的的标签
                this.allTags = this.allTags.filter(item => {
                    return _.indexOf(this.ownerTags, item.displayname) === -1
                })
            })
        },
        // 获取所有标签
        getOwnerTags () {
            getOwnerTags({ rowkey: this.rowkey, objecturi: this.objectUri }).then(response => {
                this.ownerTags = response.data ? response.data : []
                this.getAllTags()
            })
        },
        // 添加标签
        addTags () {
            if (this.currentTag === '') {
                this.$message.error('请输入标签')
                return false
            }
            tagsSaveOrUpdate({
                displayname: this.currentTag
            }).then(response => {
                this.getAllTags()
                this.addTag_visible = !this.addTag_visible
                this.currentTag = ''
            })
        },
        // 保存标签
        saveTag (tagList) {
            let params = {
                tagList: tagList,
                rowkeyList: [{ rowkey: this.rowkey, objecturi: this.objectUri }]
            }
            tagsSave(params).then(response => {
                this.handleCloseTagsDialog()
                this.$message.success('添加成功')
                this.ownerTags.push(...tagList)
                this.getAllTags()
            })
        },
        // 设置标签
        markTags () {
            let tagList = this.allTags.filter(item => item.isChecked)
            if (tagList.length === 0) {
                this.$message.error('请选择标签')
                return false
            } else {
                this.saveTag(tagList.map(item => item.displayname))
            }
        },
        // 删除用户拥有的标签
        handleRemoveOwnerTag (tag) {
            this.$confirm('确认删除该标签?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    util.remove(this.ownerTags, tag)
                    let params = {
                        tag: tag,
                        rowkey: this.rowkey,
                        objecturi: this.objectUri
                    }
                    deleteOwnerTag(params).then(response => {
                        this.$message.success('删除成功')
                        this.getAllTags()
                    })
                })
                .catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
        },
        // 删除全局标签
        handleRemoveTag (id) {
            this.$confirm('确认删除该标签?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    delTags(id).then(response => {
                        this.$message.success('删除成功')
                        this.getAllTags()
                    })
                })
                .catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
        },
        handleResize () {
            this.$refs.postingChart.resize()
            this.$refs.hotwordChart.resize()
            this.$refs.sensitivewordChart.resize()
        },
        // 对象查看
        viewObjectInfo (objectType, instanceUri, objectUri, name, displayName, icon, belongGroup) {
            let currentUrl = window.location.href
            currentUrl = currentUrl.split('#')[0]
            currentUrl = `${currentUrl}#/obj-show?objectType=${objectType}&instanceUri=${instanceUri}&objectUri=${objectUri}&displayName=${displayName}&icon=${icon}&name=${name}&belongGroup=${belongGroup}`
            window.open(currentUrl, '')
        },
        // 对象查看
        viewIntanceInfo (objectType, instanceUri, objectUri, name, displayName, icon, belongGroup) {
            let currentUrl = window.location.href
            currentUrl = currentUrl.split('#')[0]
            currentUrl = `${currentUrl}#/obj-show?objectType=${objectType}&instanceUri=${instanceUri}&objectUri=${objectUri}&displayName=${displayName}&icon=${icon}&name=${name}&belongGroup=${belongGroup}`
            // currentUrl = `${currentUrl}#/display?objectType=${objectType}&instanceUri=${instanceUri}&objectUri=${objectUri}&displayName=${displayName}&icon=${icon}&name=${name}&belongGroup=${belongGroup}`
            window.open(currentUrl, '')
        }
    }
}
